package gui.components.JXTable.test;

import sun.security.provider.PolicyFile;

import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;

/**
 * User: Programmer
 * Date: 12.05.2006
 * Time: 17:36:30
 */
public class testGuard2{

    FileOutputStream fos,fos2 = null;
    String stringLocation = new String("c:\\test.txt");
    //String stringLocation = new String("*");
    GuardedObject go;
    Guard g = new FilePermission(stringLocation,"read");
    Permission p = new FilePermission(stringLocation,"read");
    //Permission p1 = new FilePermission(stringLocation,"write");
    Policy policy = new PolicyFile();
    ProtectionDomain pdom;
    CodeSource css;

    class myPol extends Policy{
        Permissions perms = new Permissions();
        Permission perm = null;

        myPol(){
            perm = new FilePermission(stringLocation,"read");
            //perm.newPermissionCollection().add(new FilePermission(stringLocation,"read"));
            //perms.add(perm);
            //perms.add(new AllPermission());
            perms.add(new FilePermission(stringLocation,"read"));
            //perms.add(new AllPermission());
            //perm = new AllPermission();
        }

        public boolean implies(ProtectionDomain domain, Permission permission) {
            return super.implies(domain, permission);    //To change body of overridden methods use File | Settings | File Templates.
        }

        public PermissionCollection getPermissions(CodeSource codesource){
            return perms;
        }

        public void refresh(){}
    }


    class mySM extends SecurityManager{
        public void checkPermission(Permission perm) {
            super.checkPermission(perm);    //To change body of overridden methods use File | Settings | File Templates.
        }

        public void checkWrite(String file) {
            super.checkWrite(file);    //To change body of overridden methods use File | Settings | File Templates.
        }

        public void checkPropertyAccess(String key) {
            super.checkPropertyAccess(key);    //To change body of overridden methods use File | Settings | File Templates.
        }

        public void checkPackageAccess(String pkg) {
            super.checkPackageAccess(pkg);    //To change body of overridden methods use File | Settings | File Templates.
        }
    }

    public testGuard2() throws IOException, CertificateException
    {
        Policy.setPolicy(new myPol());
        System.setSecurityManager(new mySM());
        //System.getSecurityManager().
        //SecurityManager securitymanager = System.getSecurityManager();

        /*PermissionCollection pc = new Permissions();
        pc.add(p);
        CertPath certPath = new  X509CertPath(new FileInputStream(stringLocation));

        Timestamp timestamp = new Timestamp(new Date(), certPath);
        CodeSigner ccs = new CodeSigner(null, timestamp);
        CodeSigner[] csig = new CodeSigner[]{ccs};

        CodeSource cs = new CodeSource (new URL(stringLocation), csig);
        pdom = new ProtectionDomain(cs, pc);
        policy.implies(pdom,p);
        //new FilePermission("","read"));

        Policy.setPolicy(policy);*/
        //policy.implies()

        try
        {fos2 = new FileOutputStream(stringLocation);}
        catch(AccessControlException ace){
            ace.printStackTrace();
           /* StackTraceElement[] tse = ace.getStackTrace();
            for(int i=0;i<tse.length;i++){
                System.out.println(i+": "+tse[i]);
            }
            //ace.getPermission()
            System.out.println(ace.getLocalizedMessage());
            System.out.println(ace.getMessage());*/
        }
        go = new GuardedObject(fos2,p);

        //fos = new FileOutputStream();
        fos = (FileOutputStream) go.getObject();
        //fos = new FileOutputStream(stringLocation);

        //g.checkGuard(fos);
        //securitymanager.checkPermission(p1,fos2);
        fos.write(56);
        fos.close();
    }

    public static void main(String[] args) throws IOException, CertificateException
    {
        new testGuard2();
    }

}
